﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LEDHelper;
using System.Data;
using LEDDataBase;
namespace LEDManager
{
    /// <summary>
    /// Interaction logic for LEDConsole.xaml
    /// </summary>
    public partial class LEDConsole : Window
    {
        SortedDictionary<string, LEDUnit> ledList;
        LEDData leddata = new LEDData();
        LEDDataSet setLED = new LEDDataSet();
        DataTable tbLED = new DataTable();
        public TabControl TabLED
        {
            get
            {
                return (TabControl)this.FindName("TabLED");
            }
        }

        public LEDConsole()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(LEDConsole_Loaded);
        }

        void LEDConsole_Loaded(object sender, RoutedEventArgs e)
        {
            StartControl();
        }
        private void StartControl()
        {
            try
            {
                ledList = new SortedDictionary<string, LEDUnit>();
                DataSet setResult = new DataSet();
                setResult=  leddata.LoadLedData();
                foreach (DataTable tb in setResult.Tables)
                {
                    setLED.Merge(tb);
                }
                tbLED=setLED.LEDLIST;
                LEDUnit led;
                string ledid = "";
                string expression = "";
                string system = "";
                string ledkey = "";
                ///初始化所有的LED
                foreach (DataRow dwLED in tbLED.Rows)
                {
                    system = dwLED["LLSYSTEM"].ToString();
                    ledid = dwLED["LLLEDID"].ToString();
                    ledkey = system + ledid;
                    expression = "";
                    if (ledList.ContainsKey(system + ledid))
                    {
                        led = (LEDUnit)ledList[ledkey];
                    }
                    else
                    {
                        led = new LEDUnit();
                        led.SetUp(dwLED);
                        ledList.Add(ledkey, led);
                        led.Show();
                    }
                    //AddTabItem(ledkey, led);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AddTabItem(string ledkey,LEDUnit led)
        {
            try
            {
                return;
                TabItem tbLed=null;
                foreach (TabItem tb in TabLED.Items)
                {
                    if (tb.Name == "tab"+ledkey)
                    {
                        tbLed = tb;
                    }
                }
                if (tbLed == null)
                {
                    tbLed = new TabItem();
                    tbLed.Name = "tab" + ledkey;
                    tbLed.Content = led;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
